from rest_framework import routers
from .views import ProductionIssuesViewSet, IssueImageViewSet

# 尝试导入 NestedDefaultRouter，如果不存在则使用替代方案
try:
    from rest_framework_nested.routers import NestedDefaultRouter
    nested_router_available = True
except ImportError:
    nested_router_available = False

router = routers.DefaultRouter()
# 注册生产问题视图集
router.register(r'production_issues', ProductionIssuesViewSet)

# 始终注册独立的图片上传路由，用于不依赖特定生产问题的图片上传
router.register(r'issue_images', IssueImageViewSet, basename='issue-images')

if nested_router_available:
    # 注册嵌套路由用于图片操作
    production_issues_router = NestedDefaultRouter(router, r'production_issues', lookup='issue')
    production_issues_router.register(r'images', IssueImageViewSet, basename='issue-images-nested')
    
    urlpatterns = router.urls + production_issues_router.urls
else:
    urlpatterns = router.urls